require 'depac-common/constants'
require 'optparse'
require 'ostruct'
require 'yaml'

class OptionParserClass

	def initialize(commandName)
		# Get the option fillers for the command
		optionsFillers = getOptionFillersByCommandName(commandName)
		
		# The usage for the command
		usage = optionsFillers['usage']
		
		# The no of mandatory paramters expected for the command
		@no_of_mandatory_params = optionsFillers['no_of_mandatory_params']
		
		# Set the command name
		@cmdName = commandName
		
		# The set of options for the command
		optionMapArr = optionsFillers['options']
		
		# A data structure to hold the set of options
		@options = {}

		# Define the option parser
		@optparse = OptionParser.new do |opts|
		
			# Define the usage in the banner
			opts.banner = "Usage: #{usage} [options]*"
			opts.separator ""
			opts.separator "Specific options:"			  
		  
			# Iterate through the list of options and set the them
		    optionMapArr.each do |optionMap|
				short = ""
				long = ""
				description = ""
				name = ""
				defaultvalue = ""
				
				# Iterate through each of the option parameters such as -o, --o etc.
				optionMap.each do |key, value|
					if(key.eql? 'short')
						short = value
					end
					
					if(key.eql? 'long')
						long = value
					end
					
					if(key.eql? 'description')
						description = value
					end
					
					if(key.eql? 'name')
						name = value
					end
					
					if(key.eql? 'defaultvalue')
						defaultvalue = value
					end
				end
				
				# Set the default value if provided
				if(defaultvalue != "")
					@options[:"#{name}"] = defaultvalue
				end
				
				# Set the option
				opts.on(short, long, description) do |template|
					@options[:"#{name}"] = template
				end
			end
		  
      opts.separator ""
      opts.separator "Common options:"
      
			# Set the option tail for -h --help. This option is not read from the file
			opts.on_tail("-h", "--help", "Display the help and exit") do
				puts opts
				exit
			end
			
			opts.on_tail("--version", "Show version") do
        puts "Gem: #{Depac::CloudDeploymentConstants::CLOUD_DEPL_GEM_NAME}"
				puts "Version: #{CloudDeployment::VERSION}"
				puts "Build date: #{CloudDeployment::BUILD_DATE}"
				exit
			end
		end
	end

	# Parse the arguments passed in by the user, based on the option parser defined.
	def parse()
		@optparse.parse!

		# Check required conditions
		if ARGV.size < @no_of_mandatory_params
		  puts @optparse
		  exit(-1)
		end

#		p @options
#		p ARGV
		
		@arguments = ARGV
	end
	
	# Get the command name
	def getCommandName()
		return @cmdName
	end
	
	# Get the set of options parsed by the option parser
	def getOptions()
		return @options
	end
	
	# Get the remaining set of arguments (most likely the mandatory arguments)
	def getMandatoryArgs()
		return @arguments
	end
	
	# Fetch the option fillers by reading it out from the usage.yml file
	def getOptionFillersByCommandName(commandName)
	
		spec = Gem::Specification.find_by_name(Depac::CloudDeploymentConstants::CLOUD_DEPL_GEM_NAME)
		gem_root = spec.gem_dir
		usage_file_path = gem_root + Depac::CloudDeploymentConstants::USAGE_FILE_PATH
		
		usageConfig = YAML.load_file(usage_file_path)
		commandSpecificOptionFiller = usageConfig[commandName]
		commonOptionFiller = usageConfig["common"]
		
		commonOptionArr = commonOptionFiller['options']
		optionMapArr = commandSpecificOptionFiller['options']
		
		if(optionMapArr == nil)
			optionMapArr = Array.new
			commandSpecificOptionFiller['options'] = optionMapArr
		end
		
		commonOptionArr.each do |optionMap|
			optionMapArr.push(optionMap)
		end
		
		return commandSpecificOptionFiller
	end
	
end

=begin
optionsFillers = Hash.new
optionsFillers['usage'] = "createEnvironment <build-label> <config-file-path>"

optionsArray = Array.new
optionsFillers['options'] = optionsArray

optionMap = Hash.new
optionMap['short'] = "-a"
optionMap['long'] = "--abc ABC"
optionMap['name'] = "abc"
optionMap['decription'] = "Enter abc"

optionsArray.push(optionMap)

optionMap1 = Hash.new
optionMap1['short'] = "-b"
optionMap1['long'] = "--bcd BCD"
optionMap1['name'] = "bcd"
optionMap1['decription'] = "Enter bcd"

optionsArray.push(optionMap1)

option_parser_instance = OptionParserClass.new("createEnvironment")
option_parser_instance.parse()

=end
